{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc7282dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from mlxtend.frequent_patterns import apriori\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca0805e3",
   "metadata": {},
   "source": [
    "1.以\";” 为分隔符读取数据中的ID和fruit列，将fruit 列通过lambda 函数，以逗号为分割符进行切分并去除字符串中的空格，并生成矩阵df_mtix。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3ccff78",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv('./data/fruit.csv',sep=\";\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cdeb0b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "df_mtix=df['fruit'].map(lambda line:line.replace(' ','').split(','))\n",
    "df_mtix\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a263614",
   "metadata": {},
   "source": [
    "2.使用 TransactionEncoder 函数对矩阵进行拟合df_mtix，生成数据 df_te。将数据 df_te 转换为DataFrame，使用df_te.columns 为列名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5be71f9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "from mlxtend.preprocessing import TransactionEncoder\n",
    "te = TransactionEncoder()\n",
    "data_te = te.fit_transform(df_mtix)\n",
    "df_te = pd.DataFrame(data=data_te,columns=te.columns_)\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ce122df",
   "metadata": {},
   "source": [
    "3.使用 fpgrowth 算法建模发现数据中的关联规则,指定min_support=0.5,use_colnames=True 使用mlxtend库中的associatio_rules 函数来找出关联规则。指定metric为'confidence',min_threshold 为0.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0ded4fcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mlxtend.frequent_patterns import fpgrowth\n",
    "from mlxtend.frequent_patterns import association_rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e927c53",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "model = fpgrowth(df=df_te,min_support=0.5,use_colnames=True)\n",
    "rules = association_rules(df=model,metric='confidence',min_threshold=0.3,support_only=True,) \n",
    "#由考生填写\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d18042e",
   "metadata": {},
   "source": [
    "4.输出关联规则rules中，前验置信度>0.6,置信度大于0.5的项集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f525939c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "rules[(rules['antecedent support']>=0.6) & (rules['confidence']>0.5)]\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7d885ad",
   "metadata": {},
   "source": [
    "5.输出关联规则rules中，antecedents为{苹果，香蕉}的内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89ef9f01",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "rules[rules['antecedents'] == {'apple','banana'}]\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b471248",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PySpark-2.4.5",
   "language": "python",
   "name": "pyspark-2.4.5"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
